package xin.qixia.robot.core;

import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * 消息监听
 *
 * @author wangkaixin
 * @date 2025/5/23 14:41
 */
public class RobotMessageListenerProcessor implements BeanPostProcessor {

    private final RobotMessageDispatcher dispatcher;

    public RobotMessageListenerProcessor(RobotMessageDispatcher dispatcher) {
        this.dispatcher = dispatcher;
    }

    /**
     * 扫描带有 @RobotMessageListener 的 Bean 并自动注册到分发器
     *
     * @param bean
     * @param beanName
     * @return
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {
        RobotMessageListener annotation = bean.getClass().getAnnotation(RobotMessageListener.class);
        if (annotation != null && bean instanceof RobotMessageHandler) {
            dispatcher.registerListener(annotation.topic(), (RobotMessageHandler) bean);
        }
        return bean;
    }
}